/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.DataCache;
import com.cxhd.lis.model.newmodel.LisPatient;
import com.cxhd.lis.model.newmodel.LisSamplecollection;
import com.cxhd.lis.model.newmodel.lisTestResult;
import com.cxhd.lis.util.DateUtil;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/spat")
public class LisSamplePatCtrl extends Controller {
    
    private LisSamplecollection info = new LisSamplecollection();
    private LisPatient patient = new LisPatient();
    private lisTestResult lisResult = new lisTestResult();
    private Gson gson = new Gson();
    private Map<String, String> config = DataCache.getIntance().getConfig();
    
    public void index() {
        render("/view/sample_pat.jsp");
    }
    
    public void getUserList() {
        String sql = "select * ";
        String param = " from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK WHERE date(a.CUSTOM1) = curdate() ";
        // String sea = getPara("searchContent");
//        if (sea != null && sea != "") {
//            param = " from lis_samplecollection a left join lis_patient b on a.INFOPK=b.PK WHERE a.YBLB = '1' and  b.PATIENTID LIKE '%" + getPara("searchContent") + "%' or b.PATIENTNAME LIKE '%" + getPara("searchContent") + "%' order by a.CYSJ desc";
//        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", 1),
                getParaToInt("pageSize", 10), sql, param);
        renderJson(userPage);
    }
    
    public void saveInfo() {
        Map<String, String> paraMap = new HashMap<String, String>();
        for (String key : getParaMap().keySet()) {
            paraMap.put(key, getPara(key));
        }
        String info_pk = null;
        if (paraMap != null && !paraMap.isEmpty()) {
            try {
                info_pk = patient.saveInfo(paraMap);//保存病人信息
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        String flag = "true";
        String req = getSessionAttr("name");
        if (info_pk != null && paraMap.get("SAMPLENO") != null) {
            //检测SAMPLENO
            Record re = Db.findFirst("select * from lis_samplecollection where (date(CUSTOM1) = curdate() OR date(RECEIVETIME) =curdate()) and SAMPLENO= '" + paraMap.get("SAMPLENO") + "'");
            if (re != null && re.getStr("SAMPLENO") != null) {
                flag = "1";
            } else {
                String uuid = null;
                System.out.println("select * from lis_testresult where SAMPLENO= '" + paraMap.get("SAMPLENO") + "' AND date(MEASURETIME)= curdate()");
                Record sampInfo = Db.findFirst("select * from lis_testresult where SAMPLENO= '" + paraMap.get("SAMPLENO") + "' AND date(MEASURETIME)= curdate()");
                if (sampInfo != null) {
                    uuid = sampInfo.get("UUID");
                }
                paraMap.put("RECEIVETIME", DateUtil.getDateEN());
                info.updateSample(paraMap, uuid, paraMap.get("SAMPLENO"), info_pk, req);
                flag = "true";
            }
        }
        renderText(flag);
    }
    
    public void patInfoForHis() {
        
        List<Record> d = null;
        if (DataCache.getIntance().getConfig().get("hisDbFlag").equalsIgnoreCase("true")) {
            // System.out.println("select b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no where a.Patient_code like '%1610006681' and DateDiff(dd,b.Check_date,getdate())<="+day);
            //d = Db.use("hisdb").findFirst("select office as SECTION, Patient_Code as PATIENTID,Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_sex as AGE from Ut_Lis_patient_info where Patient_code like '%" + getPara("PATIENTID") + "' order by Require_Date desc ");
            //不带医生和诊断 d = Db.use("hisdb").find("select b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case Patient_sex when '1' then '女' else '男' End AS SEX,Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no where a.Patient_code like '%" + getPara("PATIENTID") + "' and DateDiff(dd,b.Check_date,getdate())<="+day);
            d = Db.use("hisdb").find(DataCache.getIntance().getSelectHisSql(getPara("PATIENTID")));
            renderJson(d);
        }
    }
    
    public void getLisDevices() {
        renderJson(Db.use("mysql").find("select s.PK DEVICEPK,s.DEVICEMODEL,s.DEVICEID,s.DEVICENAME , s.NOTES,\n"
                + "(SELECT DEVICETYPEDESC FROM lis_devicetype d WHERE d.DEVICETYPE=s.DEVICETYPE ) DEVICETYPE1 \n"
                + " from lis_deviceset s"));
    }
    
    public void getKsmc() {
        renderJson(Db.use("mysql").find("select ksmc FROM lis_ksdm"));
    }
    
    public void getSampleType() {
        renderJson(Db.use("mysql").find("select SAMPLEDESCRIBE FROM lis_sampletype"));
    }
    
    public void getXMZT() {
        renderJson(Db.use("mysql").find("select ITEMNAME from lis_charginitem ORDER BY ITEMNAME"));
    }
    
    public void getSampleNo() {
        String sample_code = getPara("code");
        renderText(lisResult.getSampleNO_for_register(sample_code));
        
    }

    public void deleteInfo() {
        String sql = "delete from lis_samplecollection where INFOPK='" + getPara("INFOPK") + "' AND SAMPLENO='" + getPara("SAMPLENO") + "' AND date(CUSTOM1) = curdate() ";
        
        Db.update(sql);
        sql = "delete from lis_patient where PK="+getPara("INFOPK");
        Db.update(sql);
        renderText("true");
    }
}
